﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SystemCenterSoa.ApiLibrary.PermissionsValidation;
using SystemCenterSoa.BaseLibrary.Tools.LogTools;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.DtoModel.SystemManager;
using SystemCenterSoa.PoModel.SystemManager;
using SystemCenterSoa.Service.SystemManager;

namespace SystemCenterSoa.ApiProject.Area.SystemManager
{
    [ApiModuleRelation("admin_user_manager")]
    public class AdminUserManagerController : SystemManagerBaseController
    {
        private AdminUserService _adminUserService;
        public AdminUserManagerController(AdminUserService adminUserService)
        {
            _adminUserService = adminUserService;
        }
        [HttpPost]
        public ResultModel<PagerResult<AdminUserPo>> SearchPage(SearchModel sm)
        {
            try
            {
                var data = _adminUserService.SearchPage(sm);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<PagerResult<AdminUserPo>>(ex);
            }
        }
        [HttpPost]
        public ResultModel<bool> AdminUserAdd(AdminUserDto view)
        {
            try
            {
                view.creator_date = DateTime.Now;
                view.creator_name = LoginUser.user_name;
                var data = _adminUserService.AdminUserAdd(view);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }

        [HttpGet]
        public ResultModel<bool> VAdmminUserName(string userName)
        {
            try
            {
                var data = _adminUserService.VAdmminUserName(userName);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }
        [HttpPost]
        public ResultModel<bool> AdminUserUpdate(AdminUserDto view)
        {
            try
            {
                view.modifi_name = LoginUser.user_name;
                view.modifi_date = DateTime.Now;
                var data = _adminUserService.AdminUserUpdate(view);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }
        [HttpGet]
        public ResultModel<AdminUserDto> GetAdminUserViewByUserName(string userName)
        {
            try
            {
                var data = _adminUserService.GetAdminUserViewByUserName(userName);
                if (data == null)
                {
                    return MyFail<AdminUserDto>("没有获取对应的用户");
                }
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<AdminUserDto>(ex);
            }
        }
        [HttpGet]
        public ResultModel<bool> AdminUserPwdUpdate(string pwd, string userName)
        {
            try
            {
                var data = _adminUserService.AdminUserPwdUpdate(pwd,userName);
                if (data)
                {
                    return MyOk(data);
                   
                }
                return MyFail<bool>("修改失败");

            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }
        [HttpGet]
        [AllowAnonymous]  //不验证登录
        public ResultModel<LoginUserDto> Login(string userName, string pwd)
        {
            try
            {
                string mess = "";
                var data = _adminUserService.Login(userName,pwd,out mess);
              
                if (data!=null)
                {
                    Log4Tools.Info($"{userName}登录成功");
                    return MyOk(data);
                    

                }
                Log4Tools.Info($"{userName}登录失败");
                return MyFail<LoginUserDto>(mess);

            }
            catch (Exception ex)
            {
                Log4Tools.Error($"{userName}登录出错",ex);
                return MyError<LoginUserDto>(ex);
            }
        }
        [HttpGet]
        [AllowAnonymous]  //不验证登录
        public ResultModel<bool> LoginOut()
        {
            try
            {
                string mess = "";
                bool data = AdminUserService.LoginOut();
                if (data)
                {
                    return MyOk(data);

                }
                return MyFail<bool>();

            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }


    }
}
